var identifier = null;
var promptStr = "identifier 를 입력하세요.";

function init() {
	_initHeader();
	
	$("#applicationId").text(application.appId);
	$("#applicationVersion").text(application.appVersion);
	$("#contentVersion").text(application.contentVersion);
	
	var array = new Array();
	
	array.push("getVersion");
	array.push("hasApp");
	array.push("installApplication");
	array.push("startApplication");
	array.push("startApplication with parameters");
	array.push("startAlopexApplication");
	array.push("startAlopexApplication with parameters");
	array.push("startWebBrowser");
	
	draw_list(array);
}

function setOnListClickHandler(idx) {
	switch(idx) {
		case 0 :
			testGetVersion();
			
		break;
		
		case 1 :
			testHasApp();
			
		break;
		
		case 2 :
			testInstallApplication();
		break;
		
		case 3 :
			testStartApplication();
			
		break;
		
		case 4 :
			testStartApplicationWithParameters();
			
		break;
		
		case 5 :
			testStartAlopexApplication();
			
		break;
		
		case 6 :
			testStartAlopexApplicationWithParameters();
			
		break;
		
		case 7 :
			application.startWebBrowser("http://www.google.com");
			
		break;
		
		default :

		break;
	}
}

function testGetVersion() {
	if(device.platformName == "Android")
		identifier = "com.skcc.applicationDTestApp";
	else if(device.platformName == "iPhone")
		identifier = "applicationDTestApp";
	else {
		alert("해당 " + device.platformName + " 은(는) application.getVersion 을 지원 하지 않습니다.");
		
		return null;
	}
	
	identifier = prompt(promptStr, identifier);
    
    if(device.platformName == "iPhone" && identifier){
        application.hasApp(identifier, hasAppCallback);
    }else if(device.platformName != "iPhone" && identifier){
        alert("getVersion : " + application.getVersion(identifier));
    }
    
}

function hasAppCallback(hasIt) {
    if(hasIt)
        alert("getVersion : " + application.getVersion(identifier));
    else
        alert("false");
}

function testHasApp() {
	if(device.platformName == "Android")
		identifier = "com.skcc.applicationDTestApp";
	else if(device.platformName == "iPhone")
		identifier = "applicationDTestApp";
	else {
		alert("해당 " + device.platformName + " 은(는) application.hasApp 을 지원 하지 않습니다.");
		
		return null;
	}
	
	identifier = prompt(promptStr, identifier);
	
	if(identifier)
		application.hasApp(identifier, testHasAppCallback);
}

function testHasAppCallback(hasIt) {
	alert(hasIt);
}

function testInstallApplication() {
	if(device.platformName == "Android") {
		var retconfirm = confirm("installApplication을 테스트하기 위한 어플리케이션을 다운로드를 하시겠습니까?");
		
		if(retconfirm) {
			commonProgress("Downloading Application..", false, null, "white");
			
			var entity = {
				"url" : "http://203.235.211.98:7000/test/ApplicationD.apk"
			};
			
			var downloadHttp = new Http();
			
			downloadHttp.download(entity, downloadSuccessCallback, downloadErrorCallback, downloadProgressCallback, downloadCancelCallback);
		}
	} else
		alert("해당 " + device.platformName + " 은(는) application.installApplication 을 지원 하지 않습니다.");
}

function downloadSuccessCallback(path) {
	platformUIComponent.dismissProgressDialog();
	alert(path + "의 어플리케이션을 설치합니다.");
	application.installApplication(path);
}

function downloadErrorCallback(http) {
	platformUIComponent.dismissProgressDialog();
	
	notifyError("download", "error code: " + http.errorCode + " errorMessage: " + http.errorMessage);
}

function downloadProgressCallback(progress) {
	platformUIComponent.setProgress(progress);
}

function downloadCancelCallback(progress) {
	alert("apk download가 취소 되었습니다.");
}

function testStartApplication() {
	if(device.platformName == "Android")
		identifier = "com.skcc.alopex.startapplicationtest";
	else if(device.platformName == "iPhone")
		identifier = "startApplicationTest://";
	else {
		alert("해당 " + device.platformName + " 은(는) application.startApplication 을 지원 하지 않습니다.");
		
		return null;
	}
	
	identifier = prompt(promptStr, identifier);
	
	if(identifier) {
		application.startApplication(identifier);
	}
}

function testStartApplicationWithParameters() {
	var parameters;
	
	if(device.platformName == "Android") {
		identifier = "com.skcc.alopex.startapplicationtest";
		
		parameters = {
			"boolean" : {
				"booleanKey" : true
			},
			"float" : {
				"floatKey" : 12.34
			},
			"int" : {
				"intKey" : 100
			},
			"string" : {
				"stringKey" : "Hello Alopex"
			}
		};
	} else if(device.platformName == "iPhone") {
		identifier = "startApplicationTest://";
		
		parameters = {
			"booleanKey" : true,
			"floatKey" : 12.34,
			"intKey" : 100,
			"stringKey" : "HelloAlopex"
		};
	} else {
		alert("해당 " + device.platformName + " 은(는) application.startApplication 을 지원 하지 않습니다.");
		
		return null;
	}
	
	identifier = prompt(promptStr, identifier);
	
	if(identifier) {
		application.startApplication(identifier, parameters);
	}
}

function testStartAlopexApplication() {
	if(device.platformName == "Android")
		identifier = "com.skcc.applicationDTestApp";
	else if(device.platformName == "iPhone")
		identifier = "applicationDTestApp";
	else {
		alert("해당 " + device.platformName + " 은(는) application.startApplication 을 지원 하지 않습니다.");
		
		return null;
	}
	
	identifier = prompt(promptStr, identifier);
	
	if(identifier) {
		application.startAlopexApplication(identifier, "sample_2");
	}
}

function testStartAlopexApplicationWithParameters() {
	if(device.platformName == "Android")
		identifier = "com.skcc.applicationDTestApp";
	else if(device.platformName == "iPhone")
		identifier = "applicationDTestApp";
	else {
		alert("해당 " + device.platformName + " 은(는) application.startApplication 을 지원 하지 않습니다.");
		
		return null;
	}
	
	identifier = prompt(promptStr, identifier);
	
	if(identifier) {
		var parameters = {
			"param01" : "111",
			"param02" : "222",
			"param03" : "333"
		};
		
		if(identifier) {
			application.startAlopexApplication(identifier, "sample_2", parameters);
		}
	}
}